<!--
 * @Author: weikang
 * @Description:
 * @Date: 2024-01-07 15:43:30
 * @LastEditTime: 2024-05-05 13:27:32
 * @FilePath: \env-manager-pc\src\views\update\manager\edit.vue
-->
<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { rules, useInitForm } from './page'

const { versionList } = defineProps({
  versionList: {
    type: Array,
    default: () => ([]),
  },
})

const emits = defineEmits(['submit'])
const dialogVisible = ref(false)
const { formData, initForm } = useInitForm()
const formRef = ref<FormInstance>()
const btnLoading = ref(false)
function handleClose() {
  close()
}

function open() {
  dialogVisible.value = true
}

function doLoading(flag: boolean) {
  btnLoading.value = flag
}

function submit() {
  formRef.value?.validate((vaild) => {
    if (vaild) {
      emits('submit', formData.value, doLoading)
    }
  })
}

function close() {
  dialogVisible.value = false
  btnLoading.value = false
  initForm()
}

defineExpose({
  open,
  close,
})
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    append-to-body
    title="升级"
    width="600px"
    :before-close="handleClose"
  >
    <ElForm ref="formRef" label-width="180px" label-position="top" label-suffix="：" :rules="rules" :model="formData">
      <ElRow :gutter="30">
        <ElCol :span="24">
          <ElFormItem label="升级方案" prop="fileID">
            <ElSelect v-model="formData.fileID" class="w-full" clearable filterable>
              <ElOption v-for="item in versionList as any" :key="item.GUID" :label="item.Name" :value="item.GUID" />
            </ElSelect>
          </ElFormItem>
        </ElCol>
      </ElRow>
    </ElForm>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit">
          开始升级
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
